<?php
include_once("../Dao/db.php");


Class ScoreRank
{
	// 表示形式
	var $reportAxis = "id";
	
	// ランキング月(年)
	var $year;
	// ランキング月(月)
	var $month;	
	// ランキング母体
	var $group = "memberId";	
	// ランキング対象データ
	var $rank_item = "impression";
	//メンバーID
	var $memberId;
	//ユーザーランク
	var $userRank;

	//企業規模ID
	var $scale;
	//企業規模名
	var $scaleName;
	//業種ID
	var $industryId;
	//業種名
	var $industryName;		
	

	var $tableList = "";
	var $tableHeader = "";	
	var $errorMsg = "";

	var $same_industry;
	var $same_scale;

	
	function MyTask()
	{

		//print_r($_POST);
		session_start();
		//業種、規模取得
		$this->getUserData();

		// 初期化の場合
		if($_POST["act"] == null)
		{
			$this->getTableList();
			$this->getTableHeader();			
		}
		// 検索の場合
		else if($_POST["act"] == "submit")
		{
			$this->iniForm();
			$this->getTableList();
			$this->getTableHeader();
		}

	}
	

	
	
	function ScoreRank()
	{
		$this->year = date("Y",time());
		$this->month = date("m",time());
	}
	
	function iniForm()
	{
		
		// ランキング月(年)
		$this->year = $_POST["year"];		
		// ランキング月(月)
		$this->month = $_POST["month"];
		//ランキング母体
		$this->group = $_POST["group"];
		//ランキング対象データ
		$this->rank_item = $_POST["rank_item"];

		$this->same_industry = $_POST['same_industry'];
		$this->same_scale = $_POST['same_scale'];

		
	}
		
	
	function getScoreRankData($type){
		//$type:1=>TOP15 ,$type2=>全データ
		if($type==1)$limitWords = "LIMIT 0,15";	
		
		$whereWords = "WHERE B.loginid IS NOT NULL AND B.joinRanking=1 AND ";
		$groupByWords = "GROUP BY ";
		$orderWords = "ORDER BY ";
	
		
		//ランキング対象月指定
		$target_date = sprintf("%04d-%02d-01",$this->year,$this->month);
		$whereWords .= "date = '$target_date' ";

		//ランキング対象データ
		$selectWords = "SUM(".$this->rank_item.")";
		$orderWords .= $this->rank_item." DESC";

		//ランキング母体
		$groupByWords .= "A.".$this->group;
		$selectWords = $this->group=='adGroup_Id' ? $this->rank_item : "SUM(".$this->rank_item.") AS ".$this->rank_item;
		if($this->group=='campaign_Id'){
			$selectWords .= ",A.campaign_Id";
		} if($this->group=='adGroup_Id'){
			$selectWords .= ",A.adGroup_Id";
		}

		//同一規模
		if($_POST['same_scale']==1)
		$whereWords .= " AND B.scale=".$this->scale;

		//同一業種
		if($_POST['same_industry']==1)
		$whereWords .=  " AND B.industryId=".$this->industryId;


		$sql = "SELECT $selectWords,B.loginid,B.memberId,B.ccStatus FROM member B  
				LEFT JOIN admonthlyreport A ON A.memberId=B.memberId $whereWords $groupByWords $orderWords";

		//echo $sql;
		$result = mysql_query($sql);
		return $result;

	}

	
	function getTableList()
	{	

		$data = $this->getScoreRankData(1);
		$this->tableList .= $tableHeader."<tr><td>";
		$this->getLeftTableList($data);
		$this->tableList .= "</td><td>";
		$data = $this->getScoreRankData(2);
		$this->getRightTableList($data);
		$this->tableList .= "</td></tr></table>";
	}
	

	//テーブル表示(トップ15)
	function getLeftTableList($data){
		$this->tableList .= "<table width=\"100%\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\" class=\"tbl\">";
		$rank = 1;
		while($row = mysql_fetch_array($data)){

			$data_f = array('ctr','cvr','cpc','roas');
			if(in_array($this->rank_item,$data_f))
				$point = sprintf("%.2f",$row[$this->rank_item]);
			else
				$point = $row[$this->rank_item];
			$this->tableList .= $this->getTrcode(0,$row['loginid'],$rank,$point);
			if($rank==15) break;
			$rank++;
		}

		if($rank!=15)
		for($i=0;$i<=15-$rank;$i++) $this->tableList .= $this->getTrcode(0,"-","-","-");



		$this->tableList .= "</table>";
	}



	function getTableHeader(){
		$data = $this->getScoreRankData(1);
		$total = mysql_num_rows($data);		
		$this->tableHeader = "<table width=\"100%\" border=\"0\"><tr><td colspan=\"2\">";
		
		if($_POST['same_industry']==1) $this->tableHeader .= "業種:".$this->industryName;
		if($_POST['same_scale']==1) $this->tableHeader .= "　規模:".$this->scaleName."　 ";
		$this->tableHeader .= $this->rank_item."ランキング<br />";

		$this->tableHeader .= $total."人中 ".($this->userRank!='' ? $this->userRank : 0)."位</td></tr>";
	}


	//テーブル表示(ログインユーザー前後15ユーザー)
	function getRightTableList($data){

		session_start();
		$this->tableList .= "<table width=\"100%\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\" class=\"tbl\">";
		$rank = 1;
		$rank_in = 0;
		$rank_suff = 0;
		$pre7_tr = array();

		if(mysql_num_rows($data)!=0){
			while($row = mysql_fetch_array($data)){
	
				$data_f = array('ctr','cvr','cpc','roas');
				if(in_array($this->rank_item,$data_f))
					$point = sprintf("%.2f",$row[$this->rank_item]);
				else
					$point = $row[$this->rank_item];
	
						
				if($rank_in==0){
					//ユーザーランキングに未出現
					//初出現までの７ユーザーデータ取得				
					array_push($pre7_tr,$this->getTrcode(0,$row['loginid'],$rank,$point));
					if(count($pre7_tr)>=9) array_shift($pre7_tr);
	
					if($row['memberId'] == $_SESSION['memberId']){
	
						//初めてランキングに出現	
						//初出現までの７ユーザーデータ表示
						//for($i=0;$i<=7-count($pre7_tr);$i++) $this->tableList .= $this->getTrcode(0,"-","-","-");
						for($i=0;$i<count($pre7_tr)-1;$i++)
							$this->tableList .= $pre7_tr[$i];
	
						//ログインユーザーデータ表示
						$rank_in =1;
						$this->userRank = $rank;
						$this->tableList .=  $this->getTrcode(1,$row['loginid'],$rank,$point);					
					}
				}else{
					//後7ユーザー表示
					$this->tableList .= $this->getTrcode(0,$row['loginid'],$rank,$point);
					$rank_suff++;
					if($rank_suff+count($pre7_tr)==15) break;
				}
				$rank++;
			}
			if($rank!=15){
				$cnt_pre = $rank_in==1 ? count($pre7_tr) : 1;
				for($i=0;$i<15-$rank_suff-$cnt_pre;$i++) $this->tableList .= $this->getTrcode(0,"-","-","-");
				if($rank_in==0) $this->tableList .= $this->getTrcode(0,$_SESSION["loginId"],"-","現在API情報を取得中 しばらくお待ち下さい。");
			}
		}else{
			for($i=0;$i<15;$i++) $this->tableList .= $this->getTrcode(0,"-","-","-");
		}
		$this->tableList .= "</table>";
	}


	function getTrcode($myrank,$loginid,$rank,$point){
		$class = $rank%2==0 ? "tr4" : "tr3";
		$td_class = $myrank==1 ? "class=\"td11\"" : "";
		$trCode .= "<tr class=\"$class\"><td  width=\"30\" class=\"td2_2\">$rank</td><td $td_class>";
		$trCode .= $this->getDispName($loginid);
		$trCode .= "</td><td $td_class><div align=\"";
		$trCode .= is_numeric($point) ? "right" : "left";
		$trCode .= "\">".$point."</div></td></tr>";

		return $trCode;
	}

	function getDispName($loginid){

		if($this->group=='memberId'){
			$dispName = $loginid."さん";
		}else if($this->group=='campaign_Id'){
			$dispName = $loginid."さんのキャンペーン";
		} if($this->group=='adGroup_Id'){
			$dispName = $loginid."さんの広告グループ";
		}

		return $dispName;
	}
	function getYearOption(){
		for($i=2008;$i<=$this->year;$i++){
			$selected = $i==$this->year ? "selected" : "";
			$option .="<option value=\"$i\" $selected>".$i."</option>";
		}
		return $option;
	}


	function getMonthOption(){
		for($i=1;$i<=12;$i++){
			$selected = $i==$this->month ? "selected" : "";
			$option .="<option value=\"$i\" $selected>".$i."</option>";
		}
		return $option;
	}


	function getTargetOption(){
		if($this->group=='memberId'){
			$option .="<option value=\"memberId\" selected>ユーザー別</option>";
			$option .="<option value=\"campaign_Id\">キャンペーン別</option>";
			$option .="<option value=\"adGroup_Id\">広告グループ別</option>";
		}else if($this->group=='campaign_Id'){
			$option .="<option value=\"memberId\">ユーザー別</option>";
			$option .="<option value=\"campaign_Id\" selected>キャンペーン別</option>";
			$option .="<option value=\"adGroup_Id\">広告グループ別</option>";
		} if($this->group=='adGroup_Id'){
			$option .="<option value=\"memberId\">ユーザー別</option>";
			$option .="<option value=\"campaign_Id\">キャンペーン別</option>";
			$option .="<option value=\"adGroup_Id\" selected>広告グループ別</option>";
		}

  		return $option;


	}

	function getItemOption(){

		$item_name = array();
		$item_name['impression'] = "Imp";
		$item_name['clickCnt'] = "Click";
		$item_name['ctr'] = "CTR[%]";
		$item_name['cpc'] = "CPC[円]";
		$item_name['cost'] = "コスト[円]";
		$item_name['cv'] = "CV";
		$item_name['cvr'] = "CVR[%]";
		$item_name['cpa'] = "CPA[円]";
		$item_name['cpm'] = "CPM[円]";
		$item_name['roas'] = "ROAS[円]";
		$item_name['sales'] = "売上[円]";
		$item_name['salesValue'] = "売上単価[円]";
		$item_name['directCvCnt'] = "直接";
		$item_name['indirectCvCnt'] = "間接";
		$item_name['asistCvCnt'] = "アシスト";
		$item_name['postCvCnt'] = "ポスト効果";

		 foreach($item_name as $key => $value){
			$selected = $key==$this->rank_item ? "selected" : "";
			$option .= "<option value=\"".$key."\" $selected>".$value." </option>";
		}

		return $option;
	}




	function getUserData(){
		$sql = "SELECT memberId,A.scale,A.industryId,scaleName,C.name AS industryName FROM member A 
				LEFT JOIN corpscale B ON A.scale=B.scaleId 
				LEFT JOIN industry C ON A.industryId=C.industryId 
				WHERE memberId=".$_SESSION["memberId"];
		$result = mysql_query($sql);
		//echo $sql;
		$row = mysql_fetch_array($result);

		$this->memberId = $row['memberId'];
		$this->scale = $row['scale'];
		$this->industryId = $row['industryId'];	
		$this->industryName = $row['industryName'];
		$this->scaleName = $row['scaleName'];
		
	}


	function getNullOutPut($value)
	{
		if($value == null || $value == "")
		{
			return "-";
		}
		else 
		{
			return $value;
		}
	}
	

	


	function getDbDate() 
	{
		$sql = "select current_date";
		$result = mysql_query($sql);		
		$rows = mysql_num_rows($result);
		if ($rows == 0) 
		{
			return "";
		} 
		else 
		{
			$nowData = mysql_fetch_row($result);
			return $nowData[0];
		}
	}



}
?>